<%@ page import="java.net.URL" %>
<%@ page import="com.micro.model.User" %>
<%@ page import="java.net.URLEncoder" %>
<%@ page import="java.net.URLDecoder" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
  String path = request.getContextPath();
  String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
//  String sourceUrl = (String) request.getParameter("sourceUrl");
//  if (null == sourceUrl) {
//    sourceUrl = basePath;
//  }
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>知识库-用户登录</title>
  <link rel="stylesheet" href="<%=basePath%>modules/micro/login/css/style.css">
  <link href="<%=basePath%>frm/bootstrap-3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body onload="queryKnowLibList();">
  <div style="height:133px;position:relative;width:100%">
    <div class="panel-heading" style="background-size:auto;background:url(<%=basePath%>modules/micro/imgs/bgimg.jpg) no-repeat;color: #ffffff;margin:auto;width:100%">
      <h3 class="panel-title">
        <a href="<%=basePath%>"><h1 id="knowTitle">浩鲸知识库</h1></a>
      </h3>
    </div>
  </div>
  <div class="parent">
    <div class="login">
      <h1>用户登录</h1>
      <%--<form method="post" action="index.html">--%>
      <p><input type="text" id="userName" name="userName" placeholder="姓名"  onkeydown="userNameKeydown(event);"/></p>
      <p><input type="password" id="userPwd" name="userPwd" placeholder="密码" onkeydown="loginKeydown(event);"/></p>
      <p>
        <select class="form-control" id="knowlib" style="width:105%">
        </select>
      </p>
      <p class="remember_me">
      <label>
      <input type="checkbox" name="remember_me" id="remember_me">
      记住密码
      </label>
      </p>
      <p class="submit"><input type="submit" onclick="login()" value="登录"></p>
      <%--</form>--%>
    </div>
  </div>
  <script src="<%=basePath%>frm/bootstrap-3.3.7/js/jquery.min.js"></script>
  <script src="<%=basePath%>frm/layer/layer.js"></script>
  <script type="text/javascript">
      var hasCook = false;
      var cookieVue = document.cookie;
      var cookieArr = decodeURI(cookieVue).split(";");
      if (cookieArr) {
          for (var i=0;i<cookieArr.length;i++) {
              if (cookieArr[i].indexOf("username") != -1) {
                  hasCook = true;
                  var uname = cookieArr[i].split("=");
                  $("#userName").val(uname[1]);
                  $("#userPwd").focus();
              }
              if (cookieArr[i].indexOf("knowlibcode") != -1) {
                  var knowlibcode = cookieArr[i].split("=");
                  $("#knowlib").val(knowlibcode[1]);
              }
          }
          if (!hasCook) {
              $("#userName").focus();
          }
      }

      function login() {
          var userName = $("#userName").val();
          var userPwd = $("#userPwd").val();
          var knowlibcode = $("#knowlib").val();
          if (userName && userPwd) {
              layer.load();
              $.post("<%=basePath%>user/logon.do", {userName: userName,userPwd:userPwd}, function result(data) {
                  layer.closeAll();
                  if ("NO" != data) {
                      layer.msg('登录成功!', {icon: 1});
                      // 写入cookie
                      document.cookie = "username="+encodeURI(userName) +";path=/; expires=Fri, 31 Dec 9999 23:59:59 GMT";
                      document.cookie = "knowlibcode="+knowlibcode+";path=/; expires=Fri, 31 Dec 9999 23:59:59 GMT";
                      window.userName = userName;
                      window.knowlibcode = knowlibcode || "changsha";
                      var urls = window.location.href.split("sourceUrl=");
                      if (urls.length > 1) {
                          window.location.href = urls[1]
                      } else {
                          window.location.href = "<%=basePath%>";
                      }

                  } else {
                      layer.msg('登录失败，请检查密码是否正确!', {icon: 5});
                  }
              });
          } else {
              layer.msg('请输入用户名或密码!', {icon: 5});
          }
      }

      /**
       * 回车事件
       * @param event
       */
      function loginKeydown(event) {
          if (event.keyCode == 13) {
              login()
          }
      }

      /**
       * 用户名回车，密码框获得焦点
       * @param event
       */
      function userNameKeydown(event) {
          if (event.keyCode == 13) {
              var userName = $("#userName").val();
              if (userName) {
                  $("#userPwd").focus();
              } else {
                  $("#userName").focus();
              }
          }
      }

      /**
       * 查询知识库
       */
      function queryKnowLibList() {
          $.post("<%=basePath%>knowLib/queryKnowLibList.do", {}, function result(data) {
              data = JSON.parse(data);
              if (data) {
                  $.each(data, function(i, item) {
                    $("<option></option>")
                      .val(item["knowCode"])
                      .text(item["knowName"])
                      .appendTo($("#knowlib"));
                  });
              }
          });
      }
  </script>
</body>
</html>
